<template>
  <div class="main-scroll">
    <headerc :title="title"></headerc>
    <el-card class="box-card">
      <!-- <div class="title-text">数据筛选</div> -->
      <div style="padding: 10px">
        <el-form
          :inline="true"
          :model="findMsg"
          class="demo-form-inline"
          label-width="70px"
        >
          <el-form-item label="员工姓名">
            <el-input
              v-model="findMsg.emplName"
              placeholder="员工姓名"
            ></el-input>
          </el-form-item>

          <el-form-item label="所属部门">
            <el-input v-model="findMsg.dept" placeholder="输入部门"></el-input>
          </el-form-item>

          <el-form-item label="二级部门">
            <el-input
              v-model="findMsg.secondDept"
              placeholder="输入二级部门"
            ></el-input>
          </el-form-item>

          <el-form-item label="所属班组">
            <el-input v-model="findMsg.team" placeholder="输入班组"></el-input>
          </el-form-item>

          <el-form-item label="发现人">
            <el-input
              v-model="findMsg.discoverer"
              placeholder="输入发现人"
            ></el-input>
          </el-form-item>

          <el-form-item label="责任人">
            <el-input
              v-model="findMsg.personLiable"
              placeholder="输入责任人"
            ></el-input>
          </el-form-item>

          <el-form-item label="发现时间">
            <el-date-picker
              v-model="findMsg.violateDate"
              type="daterange"
              range-separator="至"
              start-placeholder="起始日期"
              end-placeholder="截止日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-row>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="query"
                >查询</el-button
              >
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="reset">清除查询条件</el-button>
            </el-form-item>
          </el-row>
        </el-form>
      </div>
    </el-card>
    <el-card>
      <!-- <div class="main-seting">
        <div>
          <div class="title-text">数据列表</div> -->
           <el-button
        v-auth="'C05002'"
        style="
          float: right;
          margin-right: 20px;
          margin-bottom: 20px;
          margin-top: 20px;
        "
        icon="el-icon-download"
        @click="exportExcel"
        >导出</el-button
      >
      <el-button
      v-auth="'C05001'"
        type="primary"
        style="
          float: right;
          margin-right: 20px;
          margin-bottom: 20px;
          margin-top: 20px;
        "
        icon="el-icon-plus"
        @click="dialogVisible = true"
        >添加</el-button
      >
      <div style="margin: 20px; min-height: 55vh">
        <el-table
          :data="violateList"
          border
          style="width: 100%"
          :header-cell-style="{ background: '#F6F7FB' }"
        >
          <el-table-column
            type="index"
            :index="indexAdd"
            label="序号"
            width="50"
          >
          </el-table-column>
          <el-table-column prop="department" label="部门">
            <template slot-scope="scope">
              <p>{{ scope.row.dept }}</p>
            </template>
          </el-table-column>
          <el-table-column prop="secondaryDepartment" label="二级部门">
            <template slot-scope="scope">
              <p>{{ scope.row.secondDept }}</p>
            </template>
          </el-table-column>
          <el-table-column prop="group" label="班组">
            <template slot-scope="scope">
              <p>{{ scope.row.team }}</p>
            </template>
          </el-table-column>
          <el-table-column prop="date" label="日期" width="100">
            <template slot-scope="scope">
              <p>{{ scope.row.findTime | formatDate('yyyy-MM-dd') }}</p>
            </template>
          </el-table-column>
          <el-table-column
            prop="content"
            label="三违内容及事故内容"
            width="140"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <p>{{ scope.row.content }}</p>
            </template>
          </el-table-column>
          <el-table-column prop="emplName" label="创建人">
            <template slot-scope="scope">
              <p>{{ scope.row.emplName }}</p>
            </template>
          </el-table-column>
          <el-table-column prop="discoverer" label="发现人">
            <template slot-scope="scope">
              <p>{{ scope.row.discoverer }}</p>
            </template>
          </el-table-column>
          <el-table-column prop="liable" label="责任人">
            <template slot-scope="scope">
              <p>{{ scope.row.personLiable }}</p>
            </template>
          </el-table-column>
          <el-table-column prop="level" label="级别">
            <template slot-scope="scope">
              <p>{{ levelfun(scope.row.violateLevel) }}</p>
            </template>
          </el-table-column>
          <el-table-column prop="reviewer" label="审核人">
            <template slot-scope="scope">
              <p>{{ scope.row.reviewer }}</p>
            </template>
          </el-table-column>
          <!-- <el-table-column
                prop="score"
                label="合计得分">
              </el-table-column> -->
          <el-table-column
            prop="remark"
            label="备注"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <p>{{ scope.row.remark }}</p>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="95">
            <template slot-scope="scope">
              <el-button
                v-if="
                  scope.row.buttons == 0 ||
                  scope.row.auditStatus == 0 ||
                  scope.row.auditStatus == 1
                "
                disabled
                >审核</el-button
              >
              <el-button
                v-if="scope.row.auditStatus == 2 && scope.row.buttons == 1"
                @click="goExamine(scope.row.id)"
                >审核</el-button
              >
            </template>
          </el-table-column>
          <el-table-column prop="auditStatus" label="审核状态">
            <template slot-scope="scope">
              <p>{{ statusfun(scope.row.auditStatus) }}</p>
            </template>
          </el-table-column>
          <el-table-column
            prop="failReason"
            label="拒绝原因"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <p>{{ scope.row.failReason }}</p>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        v-if="isUpdate"
        background
        layout="prev, pager, next"
        :page-size="pageSize"
        :total="total"
        :hide-on-single-page="total < 10"
        @current-change="currentChange"
        class="pagination"
      >
      </el-pagination>
      <!-- </div>
      </div> -->
    </el-card>
    <el-dialog title="填报须知" :visible.sync="dialogVisible" width="50%">
      <div class="notice">
        <p>一、填报要求：</p>
        <p>1、及时、准确记录本人违章、发生事故或制止他人违章事件。</p>
        <p>2、简要写明反三违或反事故内容。</p>
        <p>3、各类违章要有拍照或班长签字认可。</p>
        <p>二、积分标准</p>
        <p>1、班组检查发现的违章，违章人扣2分/次，检查人加2分/次。</p>
        <p>2、车间检查发现的违章现象，扣当事人3分/次，全班每人扣1分/次。</p>
        <p>2、公司各部门检查时发现违章，全班人员当月积分清零。</p>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="
            dialogVisible = false
            dialogVisible2 = true
          "
        >
          确 定
        </el-button>
      </span>
    </el-dialog>
    <el-dialog title="添加" :visible.sync="dialogVisible2" width="50%">
      <el-form
        ref="addForm"
        :model="addForm"
        :rules="rules"
        label-width="200px"
      >
        <el-form-item label="发现时间:" prop="findTime" required>
          <el-date-picker
            v-model="addForm.findTime"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="请选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发现人:">
          <el-select
            v-model="addForm.discovererId"
            filterable
            remote
            reserve-keyword
            placeholder="请填写发现人"
            :remote-method="discovererIdRemoteMethod"
            :loading="discovererIdLoading"
            clearable
          >
            <el-option
              v-for="item in discovererIdOptions"
              :key="item.id"
              :label="item.emplName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="三违内容及事故内容:" prop="content" required>
          <el-input
            v-model="addForm.content"
            type="textarea"
            :rows="4"
            maxlength="200"
            show-word-limit
            placeholder="请填写"
          ></el-input>
        </el-form-item>
        <el-form-item label="责任人:" prop="personLiableId" required>
          <el-select
            v-model="addForm.personLiableId"
            filterable
            remote
            reserve-keyword
            placeholder="请填写责任人"
            :remote-method="personLiableIdRemoteMethod"
            :loading="personLiableIdLoading"
            clearable
          >
            <el-option
              v-for="item in personLiableIdOptions"
              :key="item.id"
              :label="item.emplName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="事故级别:" prop="violateLevel" required>
          <el-select
            v-model="addForm.violateLevel"
            placeholder="请选择"
            @change="changeLevel"
          >
            <el-option label="班组级" value="1"></el-option>
            <el-option label="车间级" value="2"></el-option>
            <el-option label="公司级" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核人:" prop="reviewerId" required>
          <el-select
            v-model="addForm.reviewerId"
            filterable
            remote
            reserve-keyword
            placeholder="请填写审核人"
            :remote-method="reviewerIdRemoteMethod"
            :loading="reviewerIdLoading"
            clearable
          >
            <el-option
              v-for="item in reviewerIdOptions"
              :key="item.id"
              :label="item.emplName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注:" prop="remark">
          <el-input
            v-model="addForm.remark"
            placeholder="请填写"
            type="textarea"
            :rows="4"
            maxlength="50"
            show-word-limit
          >
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="addViolate">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="审核" :visible.sync="dialogVisible111" width="40%">
      <el-form ref="exam" :model="exam" label-width="200px">
        <el-form-item label="审核意见:" required>
          <el-select
            v-model="exam.auditStatus"
            placeholder="请选择"
            @change="changeStatus"
          >
            <el-option label="审核通过" value="1"></el-option>
            <el-option label="审核拒绝" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="不通过理由:" required v-show="examInput">
          <el-input
            v-model="exam.failReason"
            type="textarea"
            :rows="4"
            placeholder="审核拒绝时必填"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-button @click="cancel111">取消</el-button>
      <el-button @click="exam111">确认</el-button>
    </el-dialog>
  </div>
</template>

<script>
import { getlist } from '@/api/index'
import headerc from '@/components/header.vue'
import { exportMethod } from '@/utils/utils'
import { addViolate, violateList, violateAudio } from '@/api/personal-files'
export default {
  components: {
    headerc
  },
  data() {
    return {
      isUpdate: true,
      title: '反三违及事故记录',
      userList: [],
      userList1: [],
      userList2: [],
      userList3: [],
      state: '',
      timeout: null,
      discovererIdLoading: false,
      discovererIdOptions: [],
      personLiableIdLoading: false,
      personLiableIdOptions: [],
      reviewerIdLoading: false,
      reviewerIdOptions: [],
      exam: {
        auditStatus: '',
        auditStatusNum: '',
        failReason: ''
      },
      findMsg: {
        dept: '',
        discoverer: '',
        emplName: '',
        findStartTime: '',
        findEndTime: '',
        personLiable: '',
        secondDept: '',
        team: '',
        violateDate: ''
      },
      addForm: {
        discovererId: '',
        personLiableId: '',
        reviewerId: '',
        findTime: '',
        content: '',
        violateLevel: '',
        violateLevelNum: '',
        remark: ''
      },
      rules: {
        findTime: [
          { required: true, message: '请选择日期', trigger: 'change' }
        ],
        content: [
          {
            required: true,
            message: '请输入三违内容及事故内容',
            trigger: 'blur'
          }
        ],
        personLiableId: [
          { required: true, message: '请选择责任人', trigger: 'change' }
        ],
        reviewerId: [
          { required: true, message: '请选择审核人', trigger: 'change' }
        ],
        violateLevel: [
          { required: true, message: '请选择事故级别', trigger: 'change' }
        ]
      },
      dialogVisible: false,
      dialogVisible2: false,
      dialogVisible111: false,
      violateList: [],
      pageNo: 1, // 分页页码
      pageSize: 10, // 每页条数
      total: 0,
      examId: '',
      examInput: false
    }
  },
  created() {
    this.getViolateList()
  },
  methods: {
    exportExcel() {
      const data = {
        pageNo: this.pageNo,
        pageSize: 5000,
        findStartTime: this.findMsg.violateDate[0],
        findEndTime: this.findMsg.violateDate[1],
        dept: this.findMsg.dept,
        emplName: this.findMsg.emplName,
        discoverer: this.findMsg.discoverer,
        personLiable: this.findMsg.personLiable,
        secondDept: this.findMsg.secondDept,
        team: this.findMsg.team
      }
      exportMethod('violate', data, '反三违及事故记录')
    },
    async discovererIdRemoteMethod(query) {
      if (query !== '') {
        this.discovererIdLoading = true
        const res = await getlist({
          emplName: query
        })
        if (res.code == 200) {
          console.log('res', res)
          this.discovererIdLoading = false
          this.discovererIdOptions = res.data
        }
      } else {
        this.discovererIdOptions = []
      }
    },
    async personLiableIdRemoteMethod(query) {
      if (query !== '') {
        this.personLiableIdLoading = true
        const res = await getlist({
          emplName: query
        })
        if (res.code == 200) {
          console.log('res', res)
          this.personLiableIdLoading = false
          this.personLiableIdOptions = res.data
        }
      } else {
        this.personLiableIdOptions = []
      }
    },
    async reviewerIdRemoteMethod(query) {
      if (query !== '') {
        this.reviewerIdLoading = true
        const res = await getlist({
          emplName: query
        })
        if (res.code == 200) {
          console.log('res', res)
          this.reviewerIdLoading = false
          this.reviewerIdOptions = res.data
        }
      } else {
        this.reviewerIdOptions = []
      }
    },
    // querySearchAsync(queryString, cb) {
    //   var userList = this.userList;
    //   var results = queryString
    //     ? userList.filter(this.createStateFilter(queryString))
    //     : userList;

    //   clearTimeout(this.timeout);
    //   this.timeout = setTimeout(() => {
    //     cb(results);
    //   }, 3000 * Math.random());
    // },
    // handleSelect(item) {
    //   console.log('1111',item)
    //   this.state = item.emplName;
    //   // this.emplid = item.id;
    //   // this.form11.empIds = item.id;
    // },
    // findUser(){
    //   findUser({emplName:this.state}).then(res=>{
    //     this.userList = res.data;
    //   })
    // },
    // createStateFilter(queryString) {
    //   return (state) => {
    //     console.log('11111+++',state, queryString.toLowerCase(),state.emplName.indexOf(queryString.toLowerCase()));
    //     // return (state.emplName.indexOf(queryString.toLowerCase()) ===0);
    //     return (state.emplName.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
    //   };
    // },
    // querySearchAsync1(queryString, cb) {
    //   var userList1 = this.userList1;
    //   var results = queryString
    //     ? userList1.filter(this.createStateFilter1(queryString))
    //     : userList1;

    //   clearTimeout(this.timeout);
    //   this.timeout = setTimeout(() => {
    //     cb(results);
    //   }, 3000 * Math.random());
    // },
    // handleSelect1(item) {
    //   console.log('1111',item)
    //   this.addForm.discoverer = item.emplName;
    //   this.addForm.discovererId = item.id;
    //   // this.emplid = item.id;
    //   // this.form11.empIds = item.id;
    // },
    // createStateFilter1(queryString) {
    //   return (state) => {
    //     return (state.emplName.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
    //   };
    // },
    // findUser1(){
    //   findUser({emplName:this.addForm.discoverer}).then(res=>{
    //     this.userList1 = res.data;
    //   })
    // },
    // querySearchAsync2(queryString, cb) {
    //   var userList2 = this.userList2;
    //   var results = queryString
    //     ? userList2.filter(this.createStateFilter2(queryString))
    //     : userList2;

    //   clearTimeout(this.timeout);
    //   this.timeout = setTimeout(() => {
    //     cb(results);

    //   }, 3000 * Math.random());
    // },
    // handleSelect2(item) {
    //   console.log('1111',item)
    //   this.addForm.personLiable = item.emplName;
    //   this.addForm.personLiableId = item.id;
    //   // this.emplid = item.id;
    //   // this.form11.empIds = item.id;
    // },
    // createStateFilter2(queryString) {
    //   return (state) => {
    //     return (state.emplName.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
    //   };
    // },
    // findUser2(){
    //   findUser({emplName:this.addForm.personLiable}).then(res=>{
    //     this.userList2 = res.data;
    //   })
    // },
    // querySearchAsync3(queryString, cb) {
    //   var userList3 = this.userList3;
    //   var results = queryString
    //     ? userList3.filter(this.createStateFilter3(queryString))
    //     : userList3;

    //   clearTimeout(this.timeout);
    //   this.timeout = setTimeout(() => {
    //     cb(results);
    //   }, 3000 * Math.random());
    // },
    // handleSelect3(item) {
    //   console.log('1111',item)
    //   this.addForm.reviewer = item.emplName;
    //   this.addForm.reviewerId = item.id;
    //   // this.emplid = item.id;
    //   // this.form11.empIds = item.id;
    // },
    // createStateFilter3(queryString) {
    //   return (state) => {
    //     return (state.emplName.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
    //   };
    // },
    // findUser3(){
    //   findUser({emplName:this.addForm.reviewer}).then(res=>{
    //     this.userList3 = res.data;
    //   })
    // },
    async getViolateList() {
      const res = await violateList({
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        findStartTime: this.findMsg.violateDate[0],
        findEndTime: this.findMsg.violateDate[1],
        dept: this.findMsg.dept,
        emplName: this.findMsg.emplName,
        discoverer: this.findMsg.discoverer,
        personLiable: this.findMsg.personLiable,
        secondDept: this.findMsg.secondDept,
        team: this.findMsg.team
      })
      if (res.code == 200) {
        this.isUpdate = true
        this.violateList = res.data.list
        this.total = res.data.total
      }
      console.log('this.violateList', this.violateList)
    },
    addViolate() {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          const res = await addViolate({
            discovererId: this.addForm.discovererId,
            personLiableId: this.addForm.personLiableId,
            reviewerId: this.addForm.reviewerId,
            content: this.addForm.content,
            findTime: this.addForm.findTime,
            remark: this.addForm.remark,
            violateLevel: this.addForm.violateLevelNum
          })
          if (res.code == 200) {
            this.$message({
              message: '创建成功',
              type: 'success'
            })
            this.dialogVisible2 = false
            this.getViolateList()
            this.$refs.addForm.resetFields()
            this.addForm = {
              discovererId: '',
              personLiableId: '',
              reviewerId: '',
              findTime: '',
              content: '',
              violateLevel: '',
              violateLevelNum: '',
              remark: ''
            }
          }
          console.log('res', res)
        } else {
          return false
        }
      })
    },
    cancel() {
      this.dialogVisible2 = false
      this.$refs.addForm.resetFields()
      this.addForm = {
        discovererId: '',
        personLiableId: '',
        reviewerId: '',
        findTime: '',
        content: '',
        violateLevel: '',
        violateLevelNum: '',
        remark: ''
      }
      // this.createForm.remark = ''
    },
    cancel111() {
      this.dialogVisible111 = false
      this.$refs.exam.resetFields()
      // this.createForm.remark = ''
      this.exam = {
        auditStatusNum: '',
        failReason: ''
      }
    },
    indexAdd(index) {
      return index + 1 + (this.pageNo - 1) * this.pageSize
    },
    query() {
      this.pageNo = 1
      this.isUpdate = false
      this.getViolateList()
    },
    reset() {
      this.findMsg.emplName = ''
      this.findMsg.dept = ''
      this.findMsg.secondDept = ''
      this.findMsg.team = ''
      this.findMsg.discoverer = ''
      this.findMsg.personLiable = ''
      this.findMsg.activityDate = ''
      this.state = ''
    },
    currentChange(pageNo) {
      this.pageNo = pageNo
      this.getViolateList()
    },
    levelfun(data) {
      if (data == 1) {
        return '班组级'
      } else if (data == 2) {
        return '车间级'
      } else if (data == 3) {
        return '公司级'
      }
    },
    changeLevel(e) {
      this.addForm.violateLevelNum = e
    },
    changeStatus(e) {
      this.exam.auditStatusNum = e
      if (e == 1) {
        this.examInput = false
      } else {
        this.examInput = true
      }
    },
    statusfun(data) {
      if (data == 0) {
        return '不通过'
      } else if (data == 1) {
        return '通过'
      } else if (data == 2) {
        return '待审核'
      }
    },
    goExamine(id) {
      console.log(id, '111111')
      this.dialogVisible111 = true
      this.examId = id
    },
    exam111() {
      violateAudio({
        auditStatus: this.exam.auditStatusNum,
        failReason: this.exam.failReason,
        id: this.examId
      }).then((res) => {
        console.log(res)
        this.dialogVisible111 = false
        this.getViolateList()
        this.exam = {
          auditStatusNum: '',
          failReason: ''
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.c-main-make {
  margin-left: 35px;
  height: auto;
  background: #fff;
  margin-right: 35px;
  margin-bottom: 35px;
  position: relative;
  overflow: hidden;
  border: 1px #dcdee0 solid;
}
.c-main-show {
  margin-left: 35px;
  height: auto;
  background: #fff;
  margin-right: 35px;
  margin-bottom: 35px;
  position: relative;
  // overflow: hidden;
  border: 1px #dcdee0 solid;
}
.title-text {
  width: 100%;
  height: 50px;
  border-bottom: 1px #dcdee0 solid;
  font-family: '微软雅黑 Bold', '微软雅黑 Regular', 微软雅黑, sans-serif;
  font-weight: 700;
  color: #666666;
  line-height: 50px;
  padding-left: 25px;
}
.main-scroll {
  height: 1000px;
  // overflow-y: scroll;
}
.pagination {
  display: flex;
  justify-content: end;
}
.box-card {
  margin-bottom: 20px;
}
.el-input {
  width: 250px;
}
.el-select {
  width: 250px;
}
</style>
